package ua.races.dom.docBuilder;

import org.jdom.Element;
import ua.races.vo.HorseRace;

import java.util.List;
import java.util.ListIterator;

public class HorseRaceDocumentBuilder extends DocumentBuilder {
    protected List<HorseRace> horseRaces;

    public HorseRaceDocumentBuilder(List<HorseRace> horseRaces) {
        this.horseRaces = horseRaces;
    }

    public void build() {
        HorseRace horseRace = null;

        Element root = new Element("horseRaces");

        ListIterator<HorseRace> iterator = horseRaces.listIterator();

        while (iterator.hasNext()) {
            Element horseRaceElement = new Element("horseRace");
            horseRace = iterator.next();

            Element horseRaceId = new Element("HorseRaceId");
            horseRaceId.setText(String.valueOf(horseRace.getHorseRaceId()));
            horseRaceElement.addContent(horseRaceId);

            Element raceId = new Element("RaceId");
            raceId.setText(String.valueOf(horseRace.getRaceId()));
            horseRaceElement.addContent(raceId);

            Element horseId = new Element("HorseID");
            horseId.setText(String.valueOf(horseRace.getHorseId()));
            horseRaceElement.addContent(horseId);

            Element resultPlace = new Element("ResultPlace");
            if (horseRace.getResultPlace()!=null) {
            resultPlace.setText(String.valueOf(horseRace.getResultPlace()));
            } else {
                resultPlace.setText("");
            }
            horseRaceElement.addContent(resultPlace);

            Element coef = new Element("Coef");
            if (horseRace.getCoef()!=null) {
            coef.setText(String.valueOf(horseRace.getCoef()));
            } else {
               coef.setText("");
            }
            horseRaceElement.addContent(coef);

            root.addContent(horseRaceElement);
        }

        document.addContent(root);

    }

}